﻿using System;
using GeoAPI.Geometries;
using YJKGridTopologyCal.Geometries;

namespace YJKGridTopologyCal.Triangulate
{
	public class NonEncroachingSplitPointFinder : IConstraintSplitPointFinder
	{
		public Coordinate FindSplitPoint(Segment seg, Coordinate encroachPt)
		{
			LineSegment lineSegment = seg.LineSegment;
			double num = lineSegment.Length / 2.0;
			SplitSegment splitSegment = new SplitSegment(lineSegment);
			Coordinate coordinate = NonEncroachingSplitPointFinder.ProjectedSplitPoint(seg, encroachPt);
			double num2 = coordinate.Distance(encroachPt) * 2.0 * 0.8;
			if (num2 > num)
			{
				num2 = num;
			}
			splitSegment.MinimumLength = num2;
			splitSegment.SplitAt(coordinate);
			return splitSegment.SplitPoint;
		}

		public static Coordinate ProjectedSplitPoint(Segment seg, Coordinate encroachPt)
		{
			return seg.LineSegment.Project(encroachPt);
		}
	}
}
